package com.pureut.storage.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.storage.domain.BarCodePrint;
import com.pureut.storage.domain.WmsStockInfo;
import com.pureut.storage.dto.BarCodePrintingDto;
import com.pureut.storage.dto.PrintDto;
import com.pureut.storage.mapper.BarCodePrintingMapper;
import com.pureut.storage.mapper.PurchaseReceiptMapper;
import com.pureut.storage.service.BarCodePrintingService;
import com.pureut.storage.service.WmsStockInfoService;
import com.pureut.storage.vo.BarCodePrintVo;
import com.pureut.storage.vo.PrintVo;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: CM
 * @date: 2023/4/4  14:30
 */
@Service
public class BarCodePrintingServiceImpl extends ServiceImpl<BarCodePrintingMapper, BarCodePrint> implements BarCodePrintingService {

    @Resource
    private BarCodePrintingMapper barCodePrintingMapper;

    @Resource
    private FeignService feignService;

    /**
     * 库存Service
     */
    @Resource
    private WmsStockInfoService wmsStockInfoService;

    @Resource
    private PurchaseReceiptMapper purchaseReceiptMapper;


    /**
     * 条码打印列表
     *
     * @param barCodePrintVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<BarCodePrintingDto> getList(BarCodePrintVo barCodePrintVo) {
        List<BarCodePrintingDto> list = barCodePrintingMapper.selectPrintList(barCodePrintVo);

        //根据字典查出收发类型
        List<SysDictData> jobType = DictUtils.getDictCache("job_type");
        Map<String, String> jobMap = jobType.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //根据字典查出收发类型
        List<SysDictData> printType = DictUtils.getDictCache("print_type");
        Map<String, String> printMap = printType.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));


        for (BarCodePrintingDto barCodePrintingDto : list) {
            barCodePrintingDto.setJobTypeDict(jobMap.get(barCodePrintingDto.getJobType()));
            barCodePrintingDto.setPrintTypeDict(printMap.get(barCodePrintingDto.getPrintType()));
        }

        return list;
    }

    /**
     * 来料打印
     *
     * @param materialNumber
     * @return
     */
    @Override
    public PrintDto getMaterialNumber(String materialNumber, String type) {
        PrintDto printDto = barCodePrintingMapper.selectSuppliedMaterials(materialNumber);
        if (printDto == null) {
            throw new GlobalException("请输入正确的号码");
        } else {
            return barCodePrintingMapper.selectSuppliedMaterials(materialNumber);
        }
    }

    /**
     * 打印
     *
     * @param printVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PrintVo print(PrintVo printVo) throws Exception {
        String cgrkdynum = "";
        //如果是条码添加校验是否配置了单据
        if ("2".equals(printVo.getPrintType())) {
            cgrkdynum = feignService.getAuthorityCoding("warehouse:inside:barcodePrinting:list");
            if (cgrkdynum == null) {
                throw new GlobalException("该单据未进行配置,无法生成单据编码");
            }
        }

        BarCodePrint barCodePrint = new BarCodePrint();
        BeanUtils.copyProperties(printVo, barCodePrint);
        barCodePrint.setConnectDoc(printVo.getWsiConnectNum());
        barCodePrint.setDeptId(SecurityUtils.getDeptId());
        barCodePrint.setCreateTime(new Date());
        barCodePrint.setJobType(printVo.getType());
        barCodePrint.setCreateUser(SecurityUtils.getUsername());

        if ("2".equals(printVo.getPrintType())) {
            barCodePrint.setMaterialNumber(cgrkdynum);

        }

        boolean save = save(barCodePrint);

        if (save) {
            //判断打印类型是补打还是手动添加
            if ("2".equals(printVo.getPrintType())) {

                if ("1".equals(printVo.getType())) {

                    //1是来料打印，2是水刺打印，3是脱脂打印
                    //生成物料号码

                    WmsStockInfo wmsStockInfo = new WmsStockInfo();
                    wmsStockInfo.setWsiMaterialNumber(cgrkdynum);//物料号码
                    wmsStockInfo.setWsiMaterialCode(printVo.getMaterialCode());//物料编码
                    wmsStockInfo.setWsiConnectNum(printVo.getWsiConnectNum());//关联单号
                    wmsStockInfo.setWsiMaterialStatus(2);
                    wmsStockInfo.setWsiCategory("1");
                    wmsStockInfo.setWsiMaterialNum(1);
                    printVo.setMaterialNumber(cgrkdynum);

                    //通过关联单号查出供应商编码
                    PrintDto printDto = purchaseReceiptMapper.getReceiptInfo(printVo.getWsiConnectNum());

                    wmsStockInfo.setWsiSupplierCode(printDto.getSupplier());//供应商编码
                    wmsStockInfo.setWsiGorssWeight(printVo.getGrossWeight());//毛重
                    wmsStockInfo.setWsiNetWeight(printVo.getNetWeight());//净重
                    wmsStockInfo.setWsiMaterialBatch(printVo.getMaterialBatch());//批次号
                    wmsStockInfo.setWsiProducedDate(printVo.getProductionDate());//生产日期
                    wmsStockInfoService.save(wmsStockInfo);
                    printVo.setMaterialNumber(cgrkdynum);

                } else if ("3".equals(printVo.getType())) {
                    //如果是水刺打印
                    WmsStockInfo wmsStockInfo = new WmsStockInfo();
                    wmsStockInfo.setWsiMaterialCode(printVo.getMaterialCode());
                    wmsStockInfo.setWsiConnectNum(printVo.getWsiConnectNum());//关联单号
                    wmsStockInfo.setWsiGorssWeight(printVo.getGrossWeight());//毛重
                    wmsStockInfo.setWsiNetWeight(printVo.getNetWeight());//净重
                    wmsStockInfo.setWsiWidth(printVo.getBreadth());//幅宽
                    wmsStockInfo.setWsiThick(printVo.getThick());//厚度
                    wmsStockInfo.setWsiJoint(printVo.getJoint());//接头
                    wmsStockInfo.setWsiDefect(printVo.getDefect());//疵点
                    wmsStockInfo.setWsiLength(printVo.getRollLength());//卷长
                    wmsStockInfo.setWsiDiameter(printVo.getRollingDiameter());//卷径
                    wmsStockInfo.setWsiGrawWeight(printVo.getGrawWeight());//克重
                    wmsStockInfo.setWsiMaterialNumber(cgrkdynum);//物料号码
                    wmsStockInfo.setWsiMaterialNum(1);
                    wmsStockInfo.setWsiCategory("4");
                    wmsStockInfo.setWsiMaterialStatus(2);//在库
                    wmsStockInfoService.save(wmsStockInfo);
                    printVo.setMaterialNumber(cgrkdynum);
                } else if ("5".equals(printVo.getType())) {
                    //如果是标签打印
                    WmsStockInfo wmsStockInfo = new WmsStockInfo();
                    wmsStockInfo.setWsiMaterialCode(printVo.getMaterialCode());
                    wmsStockInfo.setWsiConnectNum(printVo.getWsiConnectNum());//关联单号
                    wmsStockInfo.setWsiGorssWeight(printVo.getGrossWeight());//毛重
                    wmsStockInfo.setWsiNetWeight(printVo.getNetWeight());//净重
                    wmsStockInfo.setWsiWidth(printVo.getBreadth());//幅宽
                    wmsStockInfo.setWsiThick(printVo.getThick());//厚度
                    wmsStockInfo.setWsiJoint(printVo.getJoint());//接头
                    wmsStockInfo.setWsiLength(printVo.getRollLength());//卷长
                    wmsStockInfo.setWsiDiameter(printVo.getRollingDiameter());//卷径
                    wmsStockInfo.setWsiGrawWeight(printVo.getGrawWeight());//克重
                    wmsStockInfo.setWsiMaterialNumber(cgrkdynum);//物料号码
                    wmsStockInfo.setWsiMaterialStatus(2);//在库
                    wmsStockInfo.setWsiCategory("4");
                    wmsStockInfo.setWsiMaterialNum(1);
                    wmsStockInfo.setWsiProducedDate(printVo.getProductionDate());//生产日期
                    wmsStockInfoService.save(wmsStockInfo);
                    printVo.setMaterialNumber(cgrkdynum);
                }
            }
        } else {
            throw new GlobalException("打印失败");
        }

        return printVo;

    }


}
